package com.chuanyekeji.aokai.Controller;

import com.chuanyekeji.aokai.AokaiApplication;
import com.chuanyekeji.aokai.Entity.Menu;
import com.chuanyekeji.aokai.Entity.Product;
import com.chuanyekeji.aokai.Entity.ProductType;
import com.chuanyekeji.aokai.Mapper.MenuMapper;
import com.chuanyekeji.aokai.Mapper.ProductTypeMapper;
import com.chuanyekeji.aokai.Util.ReturnData;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class MenuController {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    ProductTypeMapper productTypeMapper;
    List<Map<String,Object>> data = new ArrayList<>();
    /**
     * 修改菜单标题
     * @param id
     * @param text
     * @return
     */
    @RequestMapping("edit")
    String edit(int id,String text) {
        Menu menu = menuMapper.selectByPrimaryKey(id);
        if(menu != null) {
            menu.setText(text);
            menuMapper.updateByPrimaryKey(menu);
            return "0";
        }
        return "";
    }

    /**
     * 获取菜单列表最大id
     * @return
     */
    @RequestMapping("getMaxNode")
    int getMaxNode() {
        return menuMapper.getMaxNode();
    }

    /**
     * 添加节点
     * @param id
     * @param text
     * @return
     */
    @RequestMapping("append")
    String append(int id,String text) {
        ProductType productType = new ProductType();
        productType.setProductTypeName(text);
        productType.setPid(0);
        productTypeMapper.insertSelective(productType);
        return ReturnData.success("添加成功");
    }

    /**
     * 删除节点
     * @param id
     * @return
     */
    @RequestMapping("remove")
    String remove(int id) {
        productTypeMapper.deleteByPrimaryKey(id);
        return ReturnData.success("删除成功");
    }
    /**
     * 获取树
     * @return
     */
    @RequestMapping("getTree")
    String getTree(HttpSession session) {
        List<ProductType> productTypeList = productTypeMapper.getProductType();
        //System.out.println(new Gson().toJson(buildTree(productTypeList)));
        List<ProductType> p = buildTree(productTypeList);
        String s = ReturnData.success("success",p);
        if(AokaiApplication.language.get(session.getId()).equals("ch")) {
            s = s.replace("productTypeName","text");
        } else {
            s = s.replace("productTypeName_en","text");
        }
        s = s.replace("productTypeName_en","text");
        System.out.println(s);
        return s;
    }
    /**
     * 获取树
     * @return
     */
    @RequestMapping("getTreeByJson")
    String getTreeByJson() {
        List<ProductType> productTypeList = productTypeMapper.getProductType();
        //System.out.println(new Gson().toJson(buildTree(productTypeList)));
        //data = data.replace("productTypeName","text");
        List<ProductType> temp = new ArrayList<>();
        for(ProductType pt : productTypeList) {
            ProductType p = new ProductType();
            p.setProductTypeName(pt.getProductTypeName() + "-" + pt.getProductTypeName_en());
            p.setId(pt.getId());
            p.setPid(pt.getPid());
            p.setChildren(pt.getChildren());
            temp.add(p);
        }
        List<ProductType> p = buildTree(temp);
        String s = ReturnData.success("success",p);
        s = s.replace("productTypeName","text");
        JSONObject obj = new JSONObject(s);
        return obj.get("data").toString();
    }

    public static List<ProductType> buildTree(List<ProductType> productTypeList) {
        List<ProductType> trees = new ArrayList<>();
        for (ProductType p : productTypeList) {
            if (p.getPid() == 0) {
                trees.add(findChildren(p,productTypeList));
            }
        }
        return trees;
    }

    public static ProductType findChildren(ProductType productType,List<ProductType> productTypeList) {
        for (ProductType p : productTypeList) {
            if(productType.getId().equals(p.getPid())) {
                if (productType.getChildren() == null) {
                    productType.setChildren(new ArrayList<>());
                }
                productType.getChildren().add(findChildren(p,productTypeList));
            }
        }
        return productType;
    }

}
